package com.basketball.gym.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.basketball.gym.mapper.UserMapper;
import com.basketball.gym.pojo.Finance;
import com.basketball.gym.mapper.FinanceMapper;
import com.basketball.gym.pojo.ResponseData;
import com.basketball.gym.pojo.User;
import com.basketball.gym.pojo.state.ResponseState;
import com.basketball.gym.service.FinanceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basketball.gym.utils.WrapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 赵肖云
 * @since 2021-03-27
 */
@Service
public class FinanceServiceImpl extends ServiceImpl<FinanceMapper, Finance> implements FinanceService {
    @Autowired
    private FinanceMapper financeMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询流水
     */
    @Override
    public ResponseData<List<Finance>> getFinance(Finance finance, User user) {
        QueryWrapper<Finance> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId());
        if (finance.getPaymentType() != null) {
            queryWrapper.eq("payment_type", finance.getPaymentType());
        }
        if (finance.getType() != null) {
            queryWrapper.eq("type", finance.getType());
        }
        List<Finance> financeList = financeMapper.selectList(queryWrapper);
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), financeList);
    }

    @Override
    public ResponseData<IPage<Finance>> adminGetFinance(Finance finance) {
        QueryWrapper<Finance> queryWrapper = new QueryWrapper<>();
        User user = null;
        if (finance.getUserId() != null) {
            queryWrapper.eq("user_id", finance.getUserId());
        }
        if (finance.getUser() != null) {
            if (finance.getUser().getPhone() != null) {
                user = userMapper.selectOne(WrapperUtils.getOneEqQueryWrapper("phone", finance.getUser().getPhone()));
                if (user != null) {
                    //如果同时传入userId和phone看是否为同一用户
                    if (finance.getUserId() != null) {
                        if (!user.getId().equals(finance.getUserId())) {
                            return new ResponseData<>("userId和phone不一致", ResponseState.PARAM_IS_ERROR.getValue());
                        }
                    } else {
                        queryWrapper.eq("user_id", user.getId());
                    }
                } else {
                    return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), null);
                }
            }
        }
        if (finance.getPaymentType() != null) {
            queryWrapper.eq("payment_type", finance.getPaymentType());
        }
        if (finance.getType() != null) {
            queryWrapper.eq("type", finance.getType());
        }
        queryWrapper.orderByDesc("create_at");
        Page<Finance> financePage = new Page<>();
        if (finance.getCurrent() != null && finance.getSize() != null) {
            financePage = new Page<>(finance.getCurrent(), finance.getSize());
        }
        IPage<Finance> financeIPage = financeMapper.selectPage(financePage, queryWrapper);
        for(Finance financeNode:financeIPage.getRecords()){
            financeNode.setUser(userMapper.selectById(financeNode.getUserId()));
        }
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(), financeIPage);
    }
}
